package com.sx.micowell.module.message

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.mvvm.base.data.DataBindingConfig
import com.example.mvvm.until.SPUtils
import com.gyf.immersionbar.ImmersionBar
import com.miloyu.modelnet.response.SysNotice
import com.sx.micowell.BR
import com.sx.micowell.R
import com.sx.micowell.databinding.ActMineNewsBinding
import com.sx.micowell.framework.app.MyActivity
import com.sx.micowell.framework.app.SPKey
import com.sx.micowell.framework.arouth.RouteManager
import com.sx.micowell.framework.arouth.RoutePath
import com.sx.micowell.framework.model.MineNewsModel

/**
 * @author: YY
 * @time: 2021/8/3
 * @describe TODO
 */
@Route(path = RoutePath.MINE_NEWS)
class MineNewsActivity : MyActivity<ActMineNewsBinding>() {

    lateinit var viewModel: MineNewsModel

    private var noticeListAdapter : NoticeListAdapter? = null

    override fun initViewModel() {
        super.initViewModel()
        viewModel = getActivityScopeViewModel(MineNewsModel::class.java)
    }

    override fun getDataBindingConfig(): DataBindingConfig {
       return DataBindingConfig(R.layout.act_mine_news, BR.viewModel, viewModel)
    }

    override fun initView() {
        ImmersionBar.with(this).statusBarDarkFont(true)
            .fitsSystemWindows(true).init()

        binding.tvAllRead.setOnClickListener {
            noticeListAdapter?.let {
                SPUtils.getInstance().put(SPKey.SP_UNREAD, false)
                binding.tvUnreadNum.text = "未读数量：0"
                it.notifyItemChanged(0)
            }

        }
    }

    override fun initData() {
        viewModel.getNotice()
    }

    override fun initViewObservable() {
        super.initViewObservable()
        // 消息公告列表
        setNoticeList()
    }

    private fun setNoticeList() {
        //取消recyclerview单独的滑动效果
        binding.recyclerNotice.isNestedScrollingEnabled = false
        //设置布局管理器
        binding.recyclerNotice.layoutManager = LinearLayoutManager(context)

        viewModel.noticeListData.observe(this) {
            if (it != null && it.size > 0 ) {
                binding.layoutEmpty.visibility = View.GONE
                if (noticeListAdapter == null) {
                    noticeListAdapter = NoticeListAdapter()
                    noticeListAdapter!!.data = it
                    binding.recyclerNotice.adapter = noticeListAdapter

                    if (SPUtils.getInstance().getBoolean(SPKey.SP_UNREAD, true)) {
                        binding.tvUnreadNum.text = "未读数量：1"
                    }
                    noticeListAdapter!!.setOnItemClickListener { adapter, view, position ->
                        if (position == 0) {
                            SPUtils.getInstance().put(SPKey.SP_UNREAD, false)
                            binding.tvUnreadNum.text = "未读数量：0"
                            adapter.notifyItemChanged(position)
                        }
                        val sysNoticesBean: SysNotice = adapter.getItem(position) as SysNotice
                        RouteManager.getInstance().intentNewsDetails(sysNoticesBean)
                    }
                } else {
                    noticeListAdapter!!.data = it
                    binding.recyclerNotice.adapter = noticeListAdapter
                }

            } else {
                binding.recyclerNotice.adapter = null
                binding.layoutEmpty.visibility = View.VISIBLE
            }
        }
    }

}